package pro.lepers.portfolio.view.header {
	import fr.digitas.flowearth.event.NodeEvent;
	import fr.digitas.flowearth.mvc.address.structs.INode;
	import fr.digitas.flowearth.mvc.address.structs.system.nodeSystem;
	import fr.digitas.flowearth.ui.text.StyledLabel;

	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;

	/**
	 * @author plepers
	 */
	public class NavItem extends Sprite {
		
		
		public var puce : MovieClip;
		public var label : StyledLabel;
		public var roll : MovieClip;


		
		
		public function NavItem () {
			addEventListener( Event.ADDED_TO_STAGE, onAdded );
			addEventListener( Event.REMOVED_FROM_STAGE, onRemoved );
			
			mouseChildren = false;
			useHandCursor = buttonMode = true;
			
			addEventListener( MouseEvent.CLICK, onClick );
			addEventListener( MouseEvent.ROLL_OVER, over );
			addEventListener( MouseEvent.ROLL_OUT, out );
		}


		private function onAdded( e : Event ) : void {
			_pNode = nodeSystem.getDefaultDevice();
			_pNode.addEventListener( NodeEvent.CHILD_CHANGE, onChildChange );
			onChildChange( null );
		}

		private function onRemoved( e : Event ) : void {
			_pNode.removeEventListener( NodeEvent.CHILD_CHANGE, onChildChange );
			_pNode = null;
		}

		public function setDatas ( datas : XML ) : void {
			
			label.styleName = ".header_navitem";
			
			label.text = datas.label.text();
			
			_nodeId = datas.@id;
		}

		private function onChildChange (event : NodeEvent) : void {
			var cnode : INode = _pNode.getCurrentChild();
			if( cnode && cnode.getId() == _nodeId ) 
				activate();
			else
				deactivate( );
		}
		
		
		private function over (event : MouseEvent) : void {
			_over = true;
			roll.visible = true;
		}

		private function out (event : MouseEvent) : void {
			_over = false;
			if( ! _active )
				roll.visible = false;
		}

		private function onClick (event : MouseEvent) : void {
			_pNode.getChild( _nodeId ).activate();
		}

		private function activate () : void {
			label.alpha = 1;
			puce.visible = true;
			_active = true;
		}

		private function deactivate () : void {
			label.alpha = 0.5;
			puce.visible = false;
			if( ! _over ) roll.visible = false;
			_active = false;
		}

		private var _nodeId : String;
		
		private var _pNode : INode;
		
		private var _over : Boolean = false;
		
		private var _active : Boolean = false;
		
	}
}
